package br.com.localfloripa.controllers;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;

import br.com.localfloripa.dao.AuditoriaDao;
import br.com.localfloripa.dto.AuditoriaDto;
import br.com.localfloripa.entities.Auditoria;
import br.com.localfloripa.services.JpaUserDetailsService;

@Named("auditoriaController")
@Scope("session")
public class AuditoriaController implements Serializable {

	private static final long serialVersionUID = 8287663370639706955L;
	private static final Logger log = LoggerFactory.getLogger(AuditoriaController.class);
	private final Integer pageSize = 10;
	
	@Inject
	@Named("auditoriaDao")
	private AuditoriaDao auditoriaDao;
	
	@Inject
	@Named("jpaUserDetailsService")
	private JpaUserDetailsService jpaUserDetailsService;
	
	/** form - atributos */
	private Auditoria auditoria;
	private LazyDataModel<AuditoriaDto> lazyModelAuditoria;	
	private String searchField;
	
	public AuditoriaController() {
		
	}
	
	@PostConstruct
	public void loadModels() {
		lazyModelAuditoria = new LazyDataModel<AuditoriaDto>() {

			@Override
			public int getPageSize() {
				return pageSize;
			}

			@Override
			public int getRowCount() {
				int count = 0;
				try {
					if (jpaUserDetailsService.isAdminUser()) {
						count = auditoriaDao.countListAuditoria().intValue();						
					} else {
						count = auditoriaDao.countListAuditoriaByLoggedUser().intValue();
					}
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
				return count; 
			}

			@Override
			public List<AuditoriaDto> load(int first, int pageSize, String sortField, SortOrder order, Map<String, String> filters) {
				List<AuditoriaDto> auditorias = null;
				try {
					if (jpaUserDetailsService.isAdminUser()) {
						auditorias = auditoriaDao.listAuditoriaPaginada(first, pageSize);
					} else {
						auditorias = auditoriaDao.listAuditoriaByLoggedUserPaginada(first, pageSize);
					}
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}				
				return auditorias;
			}			
		};
	}
	
	public void search() {	
		lazyModelAuditoria = new LazyDataModel<AuditoriaDto>() {

			@Override
			public int getPageSize() {
				return pageSize;
			}

			@Override
			public int getRowCount() {
				int count = 0;
				try {
					if (jpaUserDetailsService.isAdminUser()) {
						count = auditoriaDao.countListAuditoriaBySearch(getSearchField()).intValue();
					} else {
						count = auditoriaDao.countListAuditoriaByLoggedUserSearch(getSearchField()).intValue(); 
					}
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
				return count; 
			}

			@Override
			public List<AuditoriaDto> load(int first, int pageSize, String sortField, SortOrder order, Map<String, String> filters) {
				List<AuditoriaDto> auditorias = null;
				try {
					if (jpaUserDetailsService.isAdminUser()) {
						auditorias = auditoriaDao.listAuditoriaPaginadaBySearch(getSearchField(), first, pageSize);
					} else {
						auditorias = auditoriaDao.listAuditoriaPaginadaByLoggedUserSearch(getSearchField(), first, pageSize);
					}
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
				return auditorias;
			}			
		};
	}

	public Auditoria getAuditoria() {
		return auditoria;
	}

	public void setAuditoria(Auditoria auditoria) {
		this.auditoria = auditoria;
	}

	public LazyDataModel<AuditoriaDto> getLazyModelAuditoria() {
		return lazyModelAuditoria;
	}

	public void setLazyModelAuditoria(LazyDataModel<AuditoriaDto> lazyModelAuditoria) {
		this.lazyModelAuditoria = lazyModelAuditoria;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}
	
	

}
